package com.anolesoft.yr.mbus.contm.dto;

import com.anolesoft.yr.mbus.contm.entity.ContmBaseClause;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.google.common.collect.Lists;
import lombok.*;

import java.util.Date;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ContmBaseClauseDto {
    private String id;
    private String ppId;
    private String name;
    private String title;
    private String content;
    private String parentTId;
    private String tId;
    private Integer level;
    private String pId;
    private String pageType;

    private String type;
    private List<ContmBaseClauseDto> children;

    public List<ContmBaseClause> toContmBaseClauses(String contmBaseMainUid){

        List<ContmBaseClause> contmBaseClauses = Lists.newArrayList();
        if (children!=null)
        for (int i = 0 ; i <children.size();i++){
            ContmBaseClause contmBaseClause = new ContmBaseClause();
            excute(children.get(i),contmBaseClause,contmBaseMainUid,this.getName(),i);
            contmBaseClauses.add(contmBaseClause);
        }
        return contmBaseClauses;
    }


    public void excute(ContmBaseClauseDto contmBaseClauseDto,ContmBaseClause contmBaseClause,String contmBaseMainUid,String pageType,Integer i) {

        contmBaseClause.setObjuid(contmBaseClauseDto.getId());
        contmBaseClause.setPageType(pageType);
        contmBaseClause.setOrderCode(Byte.parseByte(Integer.toString(i)));
        contmBaseClause.setItemType("bt");
        contmBaseClause.setParentUid(contmBaseClauseDto.getPpId());
        contmBaseClause.setCTime(new Date());
        contmBaseClause.setContent(contmBaseClauseDto.getTitle());
        contmBaseClause.setAlias(contmBaseClauseDto.getName());
        contmBaseClause.setContBaseMainUid(contmBaseMainUid);

       if (contmBaseClauseDto.getContent()!=null&&!contmBaseClauseDto.getContent().equals("")){
           List<ContmBaseClause> contmBaseClauses = Lists.newArrayList();
           contmBaseClause.setContmBaseClauses(contmBaseClauses);
           contmBaseClause.getContmBaseClauses().add(
                   ContmBaseClause.builder().objuid(StringUtil.generateRundom32String()).pageType(pageType).
                           orderCode(Byte.parseByte(Integer.toString(i))).itemType("nr").parentUid(contmBaseClauseDto.id).cTime(new Date())
                           .content(contmBaseClauseDto.getContent()).alias(contmBaseClauseDto.getName()).contBaseMainUid(contmBaseMainUid).build()
           );
       }
       if (contmBaseClauseDto.getChildren()!=null){

           for (int j = 0; j < contmBaseClauseDto.getChildren().size(); j++) {
               ContmBaseClause contmBaseClauseTemp = new ContmBaseClause();
               excute(contmBaseClauseDto.getChildren().get(j),contmBaseClauseTemp,contmBaseMainUid,pageType,j );

               if (contmBaseClause.getContmBaseClauses()==null){
                   List<ContmBaseClause> objects = Lists.newArrayList();
                   contmBaseClause.setContmBaseClauses(objects);
               }
               contmBaseClause.getContmBaseClauses().add(contmBaseClauseTemp);
           }
       }


    }
}